package com.egg.auth.service.role.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egg.auth.entity.menu.Menu;
import com.egg.auth.entity.role.Role;
import com.egg.auth.entity.role.RoleLinkMenu;
import com.egg.auth.mapper.role.RoleLinkMenuMapper;
import com.egg.auth.model.request.web.RoleLinkMenuPut;
import com.egg.auth.service.menu.IMenuService;
import com.egg.auth.service.role.IRoleLinkMenuService;
import com.egg.auth.service.role.IRoleService;
import com.egg.common.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


@Service
public class RoleLinkMenuServiceImpl extends BaseServiceImpl<RoleLinkMenuMapper, RoleLinkMenu> implements IRoleLinkMenuService {


    @Autowired
    private IMenuService menuService;
    @Autowired
    private IRoleService roleService;


    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void putByRoleId(Serializable roleId, RoleLinkMenuPut param) {
        if (ObjectUtil.isEmpty(roleId)) {
            return;
        }
        Role role = roleService.baseGetById(roleId);
        delByRoleId(role.getId());
        List<Menu> menuList = menuService.baseFindById(param.getMenuIds());
        add(role.getId(), menuList);
    }

    private void add(Long roleId, List<Menu> menu) {
        if (ObjectUtil.isEmpty(roleId) || ObjectUtil.isEmpty(menu)) {
            return;
        }
        List<RoleLinkMenu> list = new ArrayList<>();
        menu.forEach(o -> {
            RoleLinkMenu roleLinkMenu = new RoleLinkMenu();
            roleLinkMenu.setRoleId(Long.parseLong(roleId.toString()));
            roleLinkMenu.setMenuId(o.getId());
            list.add(roleLinkMenu);
        });
        baseSave(list);
    }

    @Override
    public List<RoleLinkMenu> findByRoleId(Serializable roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            return Collections.emptyList();
        }
        return findByRoleId(Collections.singletonList(roleId));
    }

    @Override
    public List<RoleLinkMenu> findByRoleId(Collection<? extends Serializable> roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<RoleLinkMenu> qw = buildLambdaSortQw();
        qw.in(RoleLinkMenu::getRoleId, roleId);
        return list(qw);
    }

    private List<RoleLinkMenu> findByMenuId(Serializable menuId) {
        if (ObjectUtil.isEmpty(menuId)) {
            return Collections.emptyList();
        }
        return findByRoleId(Collections.singletonList(menuId));
    }

    private List<RoleLinkMenu> findByMenuId(Collection<? extends Serializable> menuId) {
        if (ObjectUtil.isEmpty(menuId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<RoleLinkMenu> qw = buildLambdaSortQw();
        qw.in(RoleLinkMenu::getMenuId, menuId);
        return list(qw);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void delByRoleId(Serializable roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            return;
        }
        delByRoleId(Collections.singletonList(roleId));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delByRoleId(Collection<? extends Serializable> roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            return;
        }
        LambdaQueryWrapper<RoleLinkMenu> qw = buildLambdaQw();
        qw.in(RoleLinkMenu::getRoleId, roleId);
        remove(qw);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delByMenuId(Collection<? extends Serializable> menuId) {
        if (ObjectUtil.isEmpty(menuId)) {
            return;
        }
        LambdaQueryWrapper<RoleLinkMenu> qw = buildLambdaQw();
        qw.in(RoleLinkMenu::getMenuId, menuId);
        remove(qw);
    }


}
